package com.tom.demo.controller.login;
import com.tom.demo.common.annotation.OperaLog;
import com.tom.demo.common.enumeration.OperaTypeEnum;
import com.tom.demo.common.response.Result;
import com.tom.demo.component.TokenComponent;
import com.tom.demo.dto.LoginUserDto;
import com.tom.demo.service.login.LoginService;
import com.tom.demo.vo.RouterVo;
import com.tom.demo.vo.UserInfoVo;
import io.swagger.annotations.*;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "登录授权模块")
@RestController
@RequestMapping("/authentication")
public class LoginController {

    @Resource
    private LoginService loginService;

    @Resource
    private TokenComponent tokenComponent;


    @ApiOperation(value = "登录接口")
    @PostMapping(path = {"/login"})
    public Result login(@RequestBody @Validated LoginUserDto loginUserDto){
        Map<String, String> tokenMap = loginService.login(loginUserDto);
        return Result.success(tokenMap);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping(path = {"/getUserInfo"})
    @OperaLog(operaModel = "登录授权模块",operaType = OperaTypeEnum.GET_USER_INFO,operaDesc = "获取用户信息")
    public Result getUserInfo(){
        UserInfoVo userInfoVo = loginService.getUserInfo();
        return Result.success(userInfoVo);
    }

    @ApiOperation(value = "获取路由菜单信息")
    @GetMapping(path = {"/getRouters"})
    @OperaLog(operaModel = "登录授权模块",operaType = OperaTypeEnum.GET_ROUTER_INFO,operaDesc = "获取路由菜单信息")
    public Result getRouters(){
        List<RouterVo> routerVos = loginService.getRouters();
        return Result.success(routerVos);
    }

    @ApiOperation(value = "退出登录")
    @GetMapping(path = {"/logout"})
    @OperaLog(operaModel = "登录授权模块",operaType = OperaTypeEnum.LOGOUT,operaDesc = "退出登录")
    public Result logout(HttpServletRequest request){
        SecurityContextHolder.clearContext();
        tokenComponent.removeLoginUser(request);
        return Result.success();
    }

}
